home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 32 / Amiga Format AFCD32 (Nov 1998, Issue 117).iso / -seriously_amiga- / programming / other / hrtmon / hrtmonprefs.c < prev    next >
C/C++ Source or Header  |  1998-08-10  |  10KB  |  481 lines

  1.  
  2. /*********************************/
  3. /* HRTmon preferences and loader */
  4. /*    by Hornet of Alcatraz      */
  5. /*        Alain Malek            */
  6. /*********************************/
  7. /*
  8.   This was my first C program, that's why it is so badly coded ...
  9.   Maybe the whole HRTmonPrefs program should be recoded from scratch
  10. */
  11.  
  12. // compiled with DICE
  13. // dcc HRTmonPrefs.c HRTmonGUI.o assembler.o HRTboot.o HRTmonLoad.o reqtoolss.lib
  14.  
  15.  
  16. #include "HRTmonLoad.h"
  17. #include "HRTmonGUI2.h"
  18. #include "assembler.h"
  19.  
  20. #define nb_sectors 300        // nb sectors to write for boot-disk
  21.  
  22. __far extern int BootBlock[];
  23.  
  24. #include <dos/dos.h>
  25. #include <reqtools.h>
  26. #include <proto/reqtools.h>
  27.  
  28. /*******************************************************************/
  29.  
  30. struct ReqToolsBase *ReqToolsBase;
  31.  
  32. int ShowReq(char *text,char*choice);    // Show EZRequest. from reqtools
  33.  
  34. /*******************************************************************/
  35.  
  36. void main()
  37. {
  38.     struct ExecBase *SysBase=(int*)*((int*)4);
  39.  
  40.     if (!(ReqToolsBase = (struct ReqToolsBase *)
  41.         OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION)))
  42.         exit (1);
  43.  
  44.     LoadInit();
  45.  
  46.  
  47.     MainGTags[7]=(config.autoa & 1);    // disable address field
  48.     MainGTags[10]=(config.abs & 1);
  49.     MainGTags[14]=(config.right & 1);
  50.     MainGTags[19]=config.keyboard;
  51.     MainGTags[22]=config.ide & 1;
  52.     MainGTags[25]=config.autoa & 1;
  53.     MainGTags[29]=config.key & 1;
  54.     MainGTags[32]=(config.insert & 1);
  55.     MainGTags[39]=config.delay;
  56.     MainGTags[52]=(config.lview & 1);
  57.     MainGTags[57]=config.screenmode;
  58.     MainGTags[60]=(config.vbr & 1);
  59.  
  60.     SetupScreen();
  61.     OpenMainWindow();
  62.  
  63.     sprintf( ((struct StringInfo*) (MainGadgets[GD_address] -> SpecialInfo)) -> Buffer
  64.         ,"$%x",config.address);
  65.     RefreshGList(MainGadgets[GD_address],MainWnd,NULL,1);
  66.  
  67.     if ( RemHRTmon() )
  68.         ShowReq("HRTmon located\n"
  69.             "and removed.","OK");
  70.  
  71.     do
  72.         WaitPort( MainWnd->UserPort );
  73.     while ( HandleMainIDCMP() );
  74.  
  75.     CloseMainWindow();
  76.     CloseDownScreen();
  77.  
  78.     CloseLibrary(ReqToolsBase);
  79.     exit (0);
  80.  
  81. }
  82.  
  83. /*******************************************************************/
  84.  
  85. int ShowReq(char *text,char*choice)
  86. {
  87.     struct TagItem myTags[4]={
  88.         RT_Window,NULL,
  89.         RT_LockWindow,TRUE,            // show busy pointer on parent window
  90.         RTEZ_ReqTitle,"HRTmon requester",
  91.         TAG_DONE
  92.     };
  93.  
  94.     myTags[0].ti_Data=MainWnd;    // init RT_Window field
  95.  
  96.     return ( rtEZRequest (text,choice,NULL,myTags) );
  97.  
  98. }
  99.  
  100. /*******************************************************************/
  101.  
  102. int MainCloseWindow ()
  103. {
  104.     return (FALSE);
  105. }
  106.  
  107. /*******************************************************************/
  108. // Menu save
  109.  
  110. int MainSave()
  111. {
  112. BPTR cf1,cf2;
  113.  
  114.     BPTR lock;
  115.  
  116.     memset(config.path, 0, 512);
  117.  
  118.     if ( !(cf1=Open("ENV:HRTmon.prefs",MODE_NEWFILE)) ) {
  119.         ShowReq("Couldn't save prefs.","OK");
  120.         return (TRUE);
  121.     }
  122.  
  123.     if ( !(cf2=Open("ENVARC:HRTmon.prefs",MODE_NEWFILE)) ) {
  124.         ShowReq("Couldn't save prefs.","OK");
  125.         Close (cf1);
  126.         return (TRUE);
  127.     }
  128.  
  129.  
  130.     lock = GetProgramDir();
  131.     if (lock)
  132.         NameFromLock(lock, config.path, 510);
  133.  
  134.     Write(cf1, &config, sizeof(CONFIG));
  135.     Write(cf2, &config, sizeof(CONFIG));
  136.  
  137.     Close(cf1);
  138.     Close(cf2);
  139.  
  140.     return (TRUE);
  141. }
  142.  
  143. /*******************************************************************/
  144. // Menu quit
  145.  
  146. int MainQuit()
  147. {
  148.     return (FALSE);
  149. }
  150. /*******************************************************************/
  151.  
  152. int RightClicked()
  153. {
  154.     config.right = !config.right;
  155.     return (TRUE);
  156. }
  157.  
  158. /*******************************************************************/
  159.  
  160. int BootDiskClicked()
  161. {
  162.     char *Buffer;
  163.     BPTR filelock,filehandle;
  164.     struct FileInfoBlock infoblock;
  165.     char EmptyBlock[512];
  166.     int i;
  167.     int autoaddr;
  168.  
  169.     if (config.autoa)
  170.         autoaddr = 0;
  171.     else
  172.         autoaddr = config.address;
  173.  
  174.  
  175.     if ( (filelock=Lock("HRTmon.data",ACCESS_READ)) == NULL ) {    // Lock file
  176.         ShowReq("Can't open file HRTmon.data","OK");
  177.         return (TRUE);
  178.     }
  179.  
  180.     if ( Examine(filelock,&infoblock) == FALSE ) {        // Examine file
  181.         ShowReq("Can't open file HRTmon.data","OK");
  182.         UnLock(filelock);
  183.         return (TRUE);
  184.     }
  185.  
  186.     UnLock(filelock);                    // UnLock file
  187.  
  188.     if ( (Buffer=AllocMem(infoblock.fib_Size,MEMF_ANY)) == NULL ) {
  189.         ShowReq("Not enough memory to proceed.","OK");
  190.         return (TRUE);
  191.     }
  192.  
  193.     if ( filehandle=Open("HRTmon.data",MODE_OLDFILE) ) {
  194.         Read(filehandle,Buffer,infoblock.fib_Size);        // Read file
  195.         Close(filehandle);
  196.     }
  197.     else {                        // if Can't Open
  198.         ShowReq("Can't open file HRTmon.data","OK");
  199.         return (TRUE);
  200.     }
  201.  
  202.     if ( LoadABS(Buffer,&autoaddr) == FALSE ) {
  203.         FreeMem(Buffer,infoblock.fib_Size);
  204.         return (TRUE);
  205.     }
  206.  
  207.     FreeMem(Buffer,infoblock.fib_Size);
  208.  
  209. // **** End of loading of HRTmon to 'address' *************
  210. // **** Now we will make the boot-disk ! ******************
  211.  
  212.     if (!Inhibit("DF0:",DOSTRUE)) {
  213.         ShowReq("Couldn't allocate DF0:","OK");
  214.         if (config.abs || config.autoa)
  215.         FreeMem( (APTR)autoaddr, ((ULONG*)autoaddr)[5] );
  216.         return (TRUE);    //Cancel clicked
  217.     }
  218.  
  219.  
  220.     if ( !ShowReq("Insert a formatted floppy disk in df0:\n"
  221.         "Warning ! The disk will be erased.","OK|Cancel") ) {
  222.         if (config.abs || config.autoa)
  223.         FreeMem( (APTR)autoaddr, ((ULONG*)autoaddr)[5] );
  224.         Inhibit("DF0:",FALSE);
  225.         return (TRUE);    //Cancel clicked
  226.     }
  227.  
  228.     if ( !WriteSec((char*)autoaddr,2,nb_sectors) ) {
  229.         if (config.abs || config.autoa)
  230.         FreeMem( (APTR)autoaddr, ((ULONG*)autoaddr)[5] );
  231.         ShowReq("Couldn't write to disk !","OK");
  232.         Inhibit("DF0:",FALSE);
  233.         return (TRUE);
  234.     }
  235.  
  236.     if (config.abs || config.autoa)
  237.         FreeMem( (APTR)autoaddr, ((ULONG*)autoaddr)[5] );
  238.  
  239.     BootBlock[2]=autoaddr;        // Load address for monitor
  240.     BootBlock[1]=0;            // clear checksum
  241.     BootBlock[1]=BootSum(BootBlock);
  242.  
  243.     WriteSec(BootBlock,0,2);
  244.  
  245.     for (i=0;i<512;i++)
  246.         EmptyBlock[i]=0;
  247.  
  248.     WriteSec(EmptyBlock,880,1);
  249.  
  250.     Inhibit("DF0:",FALSE);
  251.  
  252.     ShowReq ("HRTmon Boot-Disk\n"
  253.         "succesfully installed.","OK");
  254.  
  255.     return (TRUE);
  256. }
  257.  
  258. /*******************************************************************/
  259.  
  260. int InstallClicked()
  261. {
  262.     return Load("");
  263. }
  264.  
  265. /*******************************************************************/
  266.  
  267. int WriteSec(char* Buffer,int Offset,int Len) {    // Offset and Len in sector
  268.  
  269. static struct IOStdReq diskio;
  270. static struct MsgPort replyport;
  271. char *clear;
  272. int i,result;
  273.  
  274.     clear=&diskio;
  275.     for (i=0;i< sizeof(struct IOStdReq);i++)
  276.      *clear=0;
  277.  
  278.     clear=&replyport;
  279.     for (i=0;i< sizeof(struct MsgPort);i++)
  280.      *clear=0;
  281.  
  282.     replyport.mp_SigTask=FindTask( NULL );
  283.     AddPort(&replyport);
  284.  
  285.     if (OpenDevice("trackdisk.device",0,&diskio,0)) {
  286.         ShowReq("Couldn't open trackdisk.device !","OK");
  287.         RemPort(&replyport);
  288.         return (FALSE);
  289.     }
  290.  
  291.     diskio.io_Message.mn_ReplyPort=&replyport;
  292.  
  293.     diskio.io_Command=CMD_WRITE;
  294.     diskio.io_Data=Buffer;
  295.     diskio.io_Offset=Offset*512;
  296.     diskio.io_Length=Len*512;
  297.  
  298.     if ( DoIO(&diskio) )
  299.         result=FALSE;
  300.     else
  301.         result=TRUE;
  302.  
  303.     diskio.io_Command=CMD_UPDATE;
  304.     diskio.io_Data=NULL;
  305.     diskio.io_Offset=0;
  306.     diskio.io_Length=0;
  307.     DoIO(&diskio);
  308.  
  309.     diskio.io_Command=9;        // Switch motor OFF
  310.     diskio.io_Data=NULL;
  311.     diskio.io_Offset=0;
  312.     diskio.io_Length=0;
  313.     DoIO(&diskio);
  314.  
  315.     CloseDevice(&diskio);
  316.     RemPort(&replyport);
  317.  
  318.     return (result);
  319. }
  320.  
  321. /*******************************************************************/
  322.  
  323. int CancelClicked()
  324. {
  325.     return (FALSE);
  326. }
  327.  
  328. /*******************************************************************/
  329.  
  330. int addressClicked()
  331. {
  332.     char *my_string,*p;
  333.     int val=0;
  334.  
  335.     my_string=((struct StringInfo*) (MainGadgets[GD_address] -> SpecialInfo)) -> Buffer;
  336.  
  337.     if (*my_string != '$') {
  338.         sprintf(my_string,"$%x",config.address);
  339.         RefreshGList(MainGadgets[GD_address],MainWnd,NULL,1);
  340.         ShowReq("address must start with '$'","OK");
  341.         return (TRUE);
  342.     }
  343.  
  344. /* convert string in hex and check if really in hex */
  345.  
  346.     for (p=my_string+1 ; *p != 0 ; p++) {
  347.         val=val*16;
  348.         if (*p>='0' && *p<='9')
  349.         val=val+(*p-'0');
  350.         else
  351.         if (*p>='a' && *p<='f')
  352.             val=val+(*p-'a'+10);
  353.         else
  354.             if (*p>='A' && *p<='F')
  355.             val=val+(*p-'A'+10);
  356.             else {
  357.             sprintf(my_string,"$%x",config.address);
  358.             RefreshGList(MainGadgets[GD_address],MainWnd,NULL,1);
  359.             ShowReq("Not an hex number !","OK");
  360.             return(TRUE);
  361.             }
  362.  
  363.     }
  364.  
  365.     if ( val & 0xf ) {
  366.         sprintf(my_string,"$%x",config.address);
  367.         RefreshGList(MainGadgets[GD_address],MainWnd,NULL,1);
  368.         ShowReq("Not a multiple of 16 !","OK");
  369.         return(TRUE);
  370.     }
  371.  
  372.     config.address=val;
  373.  
  374.     return (TRUE);
  375. }
  376.  
  377. /*******************************************************************/
  378.  
  379. int AllocAbsClicked()
  380. {
  381.     config.abs=!config.abs;
  382.     return (TRUE);
  383. }
  384.  
  385. /*******************************************************************/
  386.  
  387. int VBRClicked()
  388. {
  389.     config.vbr=!config.vbr;
  390.     return (TRUE);
  391. }
  392.  
  393. /*******************************************************************/
  394.  
  395. int keyboardClicked()
  396. {
  397.     config.keyboard = MainMsg.Code;
  398.  
  399.     return (TRUE);
  400. }
  401.  
  402. /*******************************************************************/
  403.  
  404. int screenmodeClicked()
  405. {
  406.     config.screenmode = MainMsg.Code;
  407.  
  408.     return (TRUE);
  409. }
  410.  
  411. /*******************************************************************/
  412.  
  413. int SaveBClicked() {
  414.  
  415.     return (MainSave());
  416.  
  417. }
  418.  
  419. /*******************************************************************/
  420.  
  421. int IDEClicked()
  422. {
  423.     config.ide=!config.ide;
  424.     return (TRUE);
  425. }
  426.  
  427.  
  428. /*******************************************************************/
  429.  
  430. int InsertClicked()
  431. {
  432.     config.insert=!config.insert;
  433.     return (TRUE);
  434. }
  435.  
  436. /*******************************************************************/
  437.  
  438. int AutoAllocClicked()
  439. {
  440.     config.autoa=!config.autoa;
  441.     if (config.autoa) {
  442.         (MainGadgets[GD_address] -> Flags) |= GFLG_DISABLED;
  443.     } else {
  444.         (MainGadgets[GD_address] -> Flags) &= (~GFLG_DISABLED);
  445.     }
  446.  
  447.     RefreshGList(MainGadgets[GD_address],MainWnd,NULL,1);
  448.  
  449.     return (TRUE);
  450. }
  451.  
  452. /*******************************************************************/
  453.  
  454. int KeyClicked()
  455. {
  456.     config.key=!config.key;
  457.     return (TRUE);
  458. }
  459.  
  460. /*******************************************************************/
  461.  
  462. int repClicked()
  463. {
  464.  
  465.     config.delay = MainMsg.Code;
  466.  
  467.     return (TRUE);
  468. }
  469.  
  470.  
  471. /*******************************************************************/
  472.  
  473. int LViewClicked()
  474. {
  475.     config.lview = !config.lview;
  476.     return (TRUE);
  477. }
  478.  
  479. /*******************************************************************/
  480.  
  481.